<template>
  <div class="page-container">
    <vxe-grid ref="tableRef" v-bind="gridOptions" @sort-change="sort">
      <template #type="{ row }">
        <dict-tag :options="vendor_type" :value="row.constructionType" />
      </template>
      <template #mode="{ row }">
        <el-switch v-model="row.operationalMode" active-value="1" inactive-value="0" :before-change="() => handelModeChange(row)" />
      </template>
      <template #handle="{ row }">
        <el-button link type="primary" @click="handleDetail(row)" v-hasPermi="['biz:priceConsumer:query']">详情</el-button>
        <el-button link type="primary" @click="handleUpdate(row)" v-hasPermi="['biz:priceConsumer:edit']">编辑</el-button>
        <!-- <el-button link type="danger" @click="handleDelete(row)" v-hasPermi="['biz:priceConsumer:remove']">删除</el-button> -->
        <!-- <el-button link type="primary" @click="handleReset(row)" v-hasPermi="['system:tenant:edit']">远程复位</el-button>
          <el-button link type="primary" @click="handleAdmin(row)" v-hasPermi="['system:tenant:edit']">子账户管理</el-button> -->
      </template>
      <template #bottom>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      </template>
    </vxe-grid>
  </div>
</template>

<script setup lang="ts">
import { listUserVendor, delVendor, changeOperationalMode } from '@/api/vendor/index';
import { VendorVo } from '@/api/vendor/types';
import { getTableConfig } from './columns';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { vendor_type } = toRefs<any>(proxy?.useDict('vendor_type'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<PageQuery>({
  pageNum: 1,
  pageSize: 20,
  name: '',
  regionCode: '',
  constructionType: '',
  type: ''
});

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listUserVendor(queryParams.value);
  data.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/**
 * 排序
 * @param field
 * @param order
 */
const sort = async (field, order) => {
  let fieldName = field.field;
  if (fieldName == 'vendorName') fieldName = 'vendorId';

  queryParams.value.orderByColumn = fieldName;
  queryParams.value.isAsc = field.order;

  getList();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
/** 新增按钮操作 */
const handleAdd = () => {
  proxy?.$tab.openPage('/vendor/vendorMsg/add');
};

/** 修改按钮操作 */
const handleUpdate = async (row?: VendorVo) => {
  proxy?.$tab.openPage('/vendorInfo/vendorMsg/edit/', '', { id: row?.vendorId });
};
/** 详情按钮操作 */
const handleDetail = async (row?: VendorVo) => {
  proxy?.$tab.openPage('/vendorInfo/vendorMsg/info', '', { id: row?.vendorId });
};
/** 删除按钮操作 */
const handleDelete = async (row?: VendorVo) => {
  const _ids = row?.vendorId || '';
  await proxy?.$modal.confirm('是否确认删除"' + row?.name + '"的数据项？');
  loading.value = true;
  await delVendor(_ids).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('删除成功');
};

const handleAdmin = async (row: VendorVo) => {
  proxy?.$modal.msg('功能开发中...');
  return true;
};
const handleReset = async (row: VendorVo) => {
  proxy?.$modal.msg('功能开发中...');
  return true;
};
const handelModeChange = async (row: VendorVo) => {
  let mode = row.operationalMode == '2' ? '1' : '2';
  const res = await changeOperationalMode(row.vendorId, mode);
  if (res.code == 200) {
    proxy?.$modal.msgSuccess('操作成功！');
    return true;
  } else {
    return false;
  }
};
onMounted(() => {
  getList();
});
</script>
